﻿using StartEAS_API.Utils;
using StartsEAS_API.DAL.DALTools;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.TheApplicationFormD
{
    public class TheApplicationFormDAL
    {
        private readonly StarsEASContext _context;
        public TheApplicationFormDAL(StarsEASContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 根据Token查询该用户的所有申请表
        /// </summary>
        /// <param name="token">用户token(学生、老师)</param>
        /// <returns></returns>
        public ResultUtils GetAllApplicationFormByToken(string token)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询申请表信息---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            //判断是否为学生
            if (token.IsStudent(_context))
            {
                Model.Models.Student student = token.GetStudentByToken(_context);
                List<TheApplicationForm> theApplicationFormList = _context.TheApplicationForms.Where(t => t.ApplicantsId==student.StuId).ToList();
                AppLog.WriteInfo($"查询申请表信息---查询到{theApplicationFormList.Count()}条申请表", true);
                return new ResultUtils() { code = 200, message = theApplicationFormList };
            }
            //不是学生，则为老师
            else
            {
                Worker worker = token.GetWorkerByToken(_context);
                List<TheApplicationForm> theApplicationFormList = _context.TheApplicationForms.Where(t => t.ApplicantsId.Equals(worker.WorkerId)).ToList();
                AppLog.WriteInfo($"查询申请表信息---查询到{theApplicationFormList.Count()}条申请表", true);
                return new ResultUtils() { code = 200, message = theApplicationFormList };
            }
        }
        /// <summary>
        /// 教务处查询所有的申请表
        /// </summary>
        /// <param name="token">教务处Token</param>
        /// <returns></returns>
        public ResultUtils GetAllApplicationForm(string token)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询申请表信息---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            //判断是否为教务处
            if (user.PositionId == 6)
            {
                List<TheApplicationForm> theApplicationFormList = _context.TheApplicationForms.ToList();
                AppLog.WriteInfo($"查询申请表信息---查询到{theApplicationFormList.Count()}条申请表", true);
                return new ResultUtils() { code = 200, message = theApplicationFormList };
            }
            else
            {
                AppLog.WriteError($"查询申请表信息---非教务处账户查询", true);
                return new ResultUtils() { code = 500, message = "权限异常" };
            }
        }
        /// <summary>
        /// 添加一条申请表信息
        /// </summary>
        /// <param name="token">用户token</param>
        /// <param name="InfoJSON">申请表具体信息</param>
        /// <param name="ApplicationType">申请类型</param>
        /// <param name="Status">当前状态</param>
        /// <returns></returns>
        public ResultUtils InsertApplicationForm(string token, string InfoJSON, int ApplicationType, int Status)
        {
            StarsEASContext _context = new StarsEASContext();
            TheApplicationForm theApplicationForm = new TheApplicationForm();
            User user = _context.Users.Where(u => u.Token==token).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"新增申请表信息---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            theApplicationForm.ApplicantsId = user.Username;
            theApplicationForm.Id = 0;
            theApplicationForm.ByTheTime = DateTime.Now.AddHours(8);
            theApplicationForm.Status = Status;
            theApplicationForm.ApplicationType = ApplicationType;
            theApplicationForm.InfoJson = InfoJSON;
            _context.TheApplicationForms.Add(theApplicationForm);
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"新增申请表信息---新增失败", true);
                return new ResultUtils() { code = 500, message = "新增失败" };
            }
            AppLog.WriteInfo($"新增申请表信息---{theApplicationForm.ApplicantsId}的申请表添加成功", true);
            return new ResultUtils() { code = 200, message = result };
        }
        /// <summary>
        /// 老师审批
        /// </summary>
        /// <param name="token">老师token</param>
        /// <param name="status">1 同意 2 拒绝</param>
        /// <param name="applicationFormId">消息审批申请表Id</param>
        /// <returns></returns>
        public ResultUtils AlterStuCounselorApproval(string token, int status, int applicationFormId)
        {
            StarsEASContext _context = new StarsEASContext();
            Worker worker = token.GetWorkerByToken(_context);
            if (worker == null)
            {
                AppLog.WriteError($"老师审批申请表---老师token异常", true);
                return new ResultUtils() { code = 500, message = "token无效" };
            }
            TheApplicationForm theApplicationForm = _context.TheApplicationForms.Find(applicationFormId);
            if (theApplicationForm == null)
            {
                AppLog.WriteError($"老师审批申请表---applicationFormId有误", true);
                return new ResultUtils() { code = 500, message = "applicationFormId有误" };
            }
            if (status == 1)
            {
                AppLog.WriteInfo($"老师审批申请表---{worker.WorkerId}同意审批表{theApplicationForm.Id}", true);
                theApplicationForm.Status = (int)TheExaminationAndApproval.TheTeacherAgreedTo;
            }
            else if (status == 2)
            {
                AppLog.WriteInfo($"老师审批申请表---{worker.WorkerId}拒绝审批表{theApplicationForm.Id}", true);
                theApplicationForm.Status = (int)TheExaminationAndApproval.TheTeacherRefusedTo;
            }
            else
            {
                AppLog.WriteError($"老师审批申请表---审批状态提交有误", true);
                return new ResultUtils() { code = 500, message = "审批状态提交有误" };
            }
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteInfo($"老师审批申请表---{worker.WorkerId}审批审批表{theApplicationForm.Id}失败", true);
                return new ResultUtils() { code = 500, message = result };
            }
            AppLog.WriteInfo($"老师审批申请表---{worker.WorkerId}审批审批表{theApplicationForm.Id}成功", true);
            return new ResultUtils() { code = 200, message = result };
        }

        public ResultUtils AlterStuDeptApproval(string token, int status, TheApplicationForm data)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"教务处审批申请表信息---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            if (data == null)
            {
                AppLog.WriteError($"教务处审批申请表信息---审批表信息为空", true);
                return new ResultUtils() { code = 500, message = "theApplicationForm为空" };
            }
            TheApplicationForm theApplicationForm = _context.TheApplicationForms.Find(data.Id);
            if (status == 1)
            {
                AppLog.WriteInfo($"教务处审批申请表信息---{user.Username}同意{theApplicationForm.Id}审批表", true);
                theApplicationForm.Status = (int)TheExaminationAndApproval.DeanSOfficeAgrees;
            }
            else if (status == 2)
            {
                AppLog.WriteInfo($"教务处审批申请表信息---{user.Username}拒绝{theApplicationForm.Id}审批表", true);
                theApplicationForm.Status = (int)TheExaminationAndApproval.TheDeanSOfficeRefused;
            }
            else
            {
                AppLog.WriteInfo($"教务处审批申请表信息---审批状态提交有误", true);
                return new ResultUtils() { code = 500, message = "审批状态提交有误" };
            }
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteInfo($"教务处审批申请表---{user.Username}审批审批表{theApplicationForm.Id}失败", true);
                return new ResultUtils() { code = 500, message = result };
            }
            AppLog.WriteInfo($"教务处审批申请表---{user.Username}审批审批表{theApplicationForm.Id}成功", true);
            return new ResultUtils() { code = 200, message = result };
        }

        public ResultUtils GetStudentApplicationFormByWorkerToken(string token, string classId) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"老师查询本班审批表---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            Worker worker = token.GetWorkerByToken(_context);
            if (worker == null)
            {
                AppLog.WriteError($"老师查询本班审批表---账户{user.Username}尝试登录但不是老师", true);
                return new ResultUtils() { code = 500, message = "token无效" };
            }
            if (token.IsStudent(_context))
            {
                AppLog.WriteError($"老师查询本班审批表---账户{user.Username}学生尝试登录", true);
                return new ResultUtils() { code = 500, message = "权限异常" };
            }
            var theApplicationFormList = from theApplicationForm in _context.TheApplicationForms
                                         join student in _context.Students on theApplicationForm.ApplicantsId equals student.StuId
                                         where student.ClassId == classId
                                         select theApplicationForm;
            if (theApplicationFormList == null) {
                AppLog.WriteError($"老师查询本班审批表---未查询到任何审批表", true);
                return new ResultUtils() { code = 500, message = "未查询到任何审批表" };
            }
            return new ResultUtils() { code = 200, message = theApplicationFormList };
        }
    }
}
